package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.persistence.TableGenerator;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 *  品牌
 *
 *
 *      说明:
 *          发现请求的数据格式是ＪＳＯＮ格式
 *              原因分析：
 *                  1. axios 处理请求的原则会根据请求数据格式来定
 *                      1.1 如果请求体是对象,会转为json 发送
 *                      1.2 如果请求体是String, 会作为普通表单请求发送, 但需要我们字节保证String的格式是键值对
 *                      如: name=jack&age=12
 *
 * create by Rose on 2020/1/8
 */
@RestController
@RequestMapping("/brand")
public class BrandController {


    @Autowired
    private BrandService brandService ;


    /**
     *
     *  分页查询品牌
     *
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String  sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc,
            @RequestParam(value = "key", required = false) String key
    ){
        PageResult<Brand> result =   brandService.queryBrandByPage(page,rows,sortBy,desc,key);
       /* if( result == null){
            // 没找到，返回404
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }*/
        // 找到返回200
        return ResponseEntity.ok(result);
    }


    /**
     *
     *  新增品牌
     *
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand , @RequestParam("cids") List<Long> cids){
        this.brandService.save(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     *
     *  根据id 删除品牌
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ResponseEntity<Void> deleteById(@PathParam("id") Long id) {
        this.brandService.deleteById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * @param cid
     * @return
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandBycid(@PathVariable("cid") Long cid) {
        List<Brand> list = this.brandService.queryBrandBycid(cid);
        return ResponseEntity.ok(list);
    }


    /***
     *
     *  根据id 查询品牌
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id) {
        Brand brand = brandService.queryById(id);
        return ResponseEntity.ok(brand);
    }


    /**
     * 根据品牌ids 列表批量查询
     *
     * @param ids
     * @return
     */
    @GetMapping("brands")
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        List<Brand> brandList = this.brandService.queryBrandByIds(ids);
        return ResponseEntity.ok(brandList);
    }


}
